package com.ruoyi.web.controller.system;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysIndustry;
import com.ruoyi.system.service.ISysIndustryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 行业字典Controller
 *
 * @author ruoyi
 * @date 2025-08-14
 */
@Api(tags="行业字典控制器")
@RestController
@RequestMapping("/system/industry" )
public class SysIndustryController extends BaseController {
    /**
     * 配置表别名 默认空
     */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private ISysIndustryService sysIndustryService;

    /**
     * 查询行业字典列表
     */
    @ApiOperation(value = "查询行业字典",notes = "行业字典管理")
    @PreAuthorize("@ss.hasPermi('system:industry:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(SysIndustry sysIndustry,QueryWrapper<SysIndustry> queryWrapper) {
        setQueryCondition(sysIndustry,queryWrapper);
        startPage();
        List<SysIndustry> list = sysIndustryService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(SysIndustry sysIndustry, QueryWrapper<SysIndustry> queryWrapper) {
        if(StrUtil.isNotBlank(sysIndustry.getIndustryCode())){
            queryWrapper.eq(TABLE_PREFIX+"industry_code" ,sysIndustry.getIndustryCode());
        }
        if(StrUtil.isNotBlank(sysIndustry.getStatus())){
            queryWrapper.eq(TABLE_PREFIX+"status" ,sysIndustry.getStatus());
        }
        if(StrUtil.isNotBlank(sysIndustry.getLevel())){
            queryWrapper.eq(TABLE_PREFIX+"level" ,sysIndustry.getLevel());
        }
        if(StrUtil.isNotBlank(sysIndustry.getFullPath())){
            queryWrapper.eq(TABLE_PREFIX+"full_path" ,sysIndustry.getFullPath());
        }
        if(StrUtil.isNotBlank(sysIndustry.getRelSiteNum())){
            queryWrapper.eq(TABLE_PREFIX+"rel_site_num" ,sysIndustry.getRelSiteNum());
        }
        if(StrUtil.isNotBlank(sysIndustry.getRelProductNum())){
            queryWrapper.eq(TABLE_PREFIX+"rel_product_num" ,sysIndustry.getRelProductNum());
        }
        if(StrUtil.isNotBlank(sysIndustry.getRelUserNum())){
            queryWrapper.eq(TABLE_PREFIX+"rel_user_num" ,sysIndustry.getRelUserNum());
        }
        if(StrUtil.isNotBlank(sysIndustry.getCreaterName())){
            queryWrapper.like(TABLE_PREFIX+"creater_name" ,sysIndustry.getCreaterName());
        }
        if(sysIndustry.getDeleted() != null){
            queryWrapper.eq(TABLE_PREFIX+"deleted" ,sysIndustry.getDeleted());
        }
        if(StrUtil.isNotBlank(sysIndustry.getVersion())){
            queryWrapper.eq(TABLE_PREFIX+"version" ,sysIndustry.getVersion());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出行业字典列表
     */
    @PreAuthorize("@ss.hasPermi('system:industry:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, SysIndustry sysIndustry,QueryWrapper<SysIndustry> queryWrapper) {
        if (CollUtil.isEmpty( sysIndustry.getIds())){
            setQueryCondition(sysIndustry,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",sysIndustry.getIds());
        }
        List<SysIndustry> list = sysIndustryService.list(queryWrapper);
        ExcelUtil<SysIndustry> util = new ExcelUtil<SysIndustry>(SysIndustry. class);
        util.exportExcel(response,list, "industry");
    }

    /**
     * 获取行业字典详细信息
     */
    @ApiOperation(value = "获取行业字典",notes = "行业字典管理")
    @PreAuthorize("@ss.hasPermi('system:industry:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(sysIndustryService.queryGetById(id));
    }

    /**
     * 新增行业字典
     */
    @ApiOperation(value = "新增行业字典",notes = "行业字典管理")
    @PreAuthorize("@ss.hasPermi('system:industry:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysIndustry sysIndustry) {
        return toAjax(sysIndustryService.addSysIndustry(sysIndustry) ? 1 : 0);
    }

    /**
     * 修改行业字典
     */
    @ApiOperation(value = "修改行业字典",notes = "行业字典管理")
    @PreAuthorize("@ss.hasPermi('system:industry:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody SysIndustry sysIndustry) {
        return toAjax(sysIndustryService.updateSysIndustry(sysIndustry) ? 1 : 0);
    }


    @GetMapping("/nodeList")
    public AjaxResult getnodeList(){
        return AjaxResult.success(sysIndustryService.getNodeTree());
    }

    /**
     * 删除行业字典
     */
    @ApiOperation(value = "删除行业字典",notes = "行业字典管理")
    @PreAuthorize("@ss.hasPermi('system:industry:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysIndustryService.deleteSysIndustryByIds(ids) ? 1 : 0);
    }
}
